package com.yjt.admin.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yjt.admin.system.entity.Role;
import com.yjt.admin.system.form.RoleForm;
import com.yjt.admin.system.param.RoleParam;
import com.yjt.admin.system.service.EmpRoleService;
import com.yjt.admin.system.service.RoleService;
import com.yjt.admin.system.vo.EmplyVO;
import com.yjt.admin.system.vo.RoleVO;
import com.yjt.common.enums.ModulEnum;
import com.yjt.common.enums.OperTypeEnum;
import com.yjt.common.log.annotation.OperLog;
import com.yjt.common.utils.CopyObject;
import com.yjt.common.vo.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2021-10-08
 */
@RefreshScope // 支持Nacos的动态刷新功能
@RestController
@RequestMapping("/admin/system/role")
@Api(tags = "角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private EmpRoleService empRoleService;

    @PostMapping("/add")
    @ApiOperation(value = "增加角色信息", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.ROLE_MODUL, operType = OperTypeEnum.ADD, operDesc = "新增角色信息")
    public CommonResult addRole(@RequestBody RoleForm roleForm) {

        return CommonResult.success("角色新增成功");
    }


    @DeleteMapping("/delete")
    @ApiOperation(value = "删除角色信息", notes = "返回JSONObject", hidden = true)
    @ApiImplicitParam(name = "roleId", value = "角色id")
    @OperLog(operModul = ModulEnum.MENU_MODUL, operType = OperTypeEnum.DEL, operDesc = "删除角色信息")
    public CommonResult delRole(String roleId) {
        roleService.removeById(roleId);
        return CommonResult.success("角色删除成功");
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改角色信息", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.MENU_MODUL, operType = OperTypeEnum.MOD, operDesc = "修改角色信息")
    public CommonResult updateRole(@RequestBody RoleForm roleForm) {
        Role role = new Role();
        BeanUtils.copyProperties(roleForm, role);
        roleService.updateById(role);
        return CommonResult.success("角色修改成功");
    }

    @GetMapping("/{roleId}")
    @ApiOperation(value = "查询角色信息", notes = "返回JSONObject")
    @ApiImplicitParam(name = "roleId", value = "角色id")
    public CommonResult<RoleVO> getOneRole(@PathVariable String roleId) {
        RoleVO roleVO = new RoleVO();
        Role role = roleService.getById(roleId);
        BeanUtils.copyProperties(role, roleVO);
        return CommonResult.success(roleVO, "角色查询成功");
    }

    @SneakyThrows
    @PostMapping("/list-page")
    @ApiOperation(value = "查询分页角色信息", notes = "返回JSONObject")
    public CommonResult<IPage<RoleVO>> getPageRole(@RequestBody RoleParam roleParam) {
        IPage rolePage = roleService.page(roleParam.toPage());
        List<RoleVO> roleVOList = CopyObject.copyList(rolePage.getRecords(), RoleVO.class);
        rolePage.setRecords(roleVOList);
        return CommonResult.success(rolePage, "分页角色信息查询成功");
    }


    @GetMapping("/emply/{roleId}")
    @ApiOperation(value = "通过角色查询关联人员信息", notes = "返回JSONObject")
    @ApiImplicitParam(name = "roleId", value = "角色id")
    public CommonResult<List<EmplyVO>> getEmpRole(@PathVariable String roleId) {
        return CommonResult.success(empRoleService.getEmpRole(roleId), "角色关联人员信息查询成功");
    }

}
